package com.niodata.dp.core.usermgr.dao;

import com.niodata.dp.core.usermgr.enums.AuthType;
import com.niodata.dp.core.usermgr.enums.GroupRole;
import com.niodata.dp.core.usermgr.model.GroupInfo;
import com.niodata.dp.core.usermgr.model.GroupProfile;
import com.niodata.dp.core.usermgr.model.UserInfo;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * Created by ndp on 17-3-8.
 */
@Mapper
public interface GroupInfoMapper {

  void insert(@Param("groupInfo") GroupInfo groupInfo);

  void delete(@Param("groupId") Long groupId);

  void update(@Param("groupInfo") GroupInfo groupInfo);

  void updateStatus(@Param("groupId") Long groupId, @Param("active") Integer active);

  void updateHdfsUserAndReaderUser(@Param("groupId") Long groupId,
        @Param("hdfsUser") String hdfsUser, @Param("readerUser") String readerUser);

  GroupInfo getGroupInfo(@Param("groupId") Long groupId);

  GroupInfo getGroupInfoByName(@Param("groupName") String groupName);

  List<GroupInfo> getGroupInfoList(@Param("offset") int offset, @Param("size") int size);

  Integer getGroupInfoCount();

  List<GroupInfo> getMasterGroupInfo(@Param("userId") Long userId);

  List<GroupInfo> getOwnerOrMasterGroupInfo(@Param("userId") Long userId);

  List<GroupInfo> getUserGroupInfoList(@Param("userId") Long userId,
        @Param("role") GroupRole groupRole);

  List<GroupInfo> getAllGroup(@Param("userId") Long userId);

  GroupInfo resolveResource(@Param("authType") AuthType authType,
        @Param("authPath") String authPath);

  UserInfo getGroupOwner(@Param("groupId") Long groupId);

  @Select("select * from GROUP_INFO where ACTIVE = 1 and GROUP_NAME LIKE CONCAT('%',#{name},'%')")
  @ResultMap("groupInfoResult")
  List<GroupInfo> searchGroupByName(@Param("name") String name);


  @Insert("insert into GROUP_PROFILE(GROUP_ID,PROFILE,TYPE) values (#{p.groupId},"
        + "#{p.profiles},#{p.type})")
  void insertGroupProfile(@Param("p") GroupProfile p);

  @Update("update GROUP_PROFILE set PROFILE=#{p.profiles} "
        + "where GROUP_ID=#{p.groupId} and TYPE=#{p.type} ")
  void updateGroupProfile(@Param("p") GroupProfile p);


}
